package com.learn.api.gateway;

import com.learn.api.gateway.filters.ConfigGatewayFilterFactory;
import com.learn.api.gateway.filters.GatewayTestFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

@EnableDiscoveryClient
@SpringBootApplication
public class ApiGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }

    @Bean
    public ConfigGatewayFilterFactory exampleGatewayFilterFactory(){
        return new ConfigGatewayFilterFactory();
    }

    /**
     * 也可以在yml文件中进行配置
     * @param builder
     * @return
     */
    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder){
        return builder.routes()
                .route("learn-service-second-route",r->r.path("/learn-service-second/**")
                        .filters(f-> f.stripPrefix(1).filter(new GatewayTestFilter()))
                        .uri("lb://learn-service-second-service"))
                /*.route("matmanager-service-route",r->r.path("/matmanager-service/**")
                        .filters(f-> f.stripPrefix(1))
                        .uri("lb://matmanager-service"))*/
                .build();
    }

}
